package com.googlecode.wheres_my_stuff_okay_potato;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Activity that holds the user's dashboard, holding almost all of the main
 * functionality in the program.
 */
public class DashboardActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dashboard);
		final Global GLOBAL = (Global) getApplication();
		final Context context = DashboardActivity.this;

		// --- Standard UI ---

		final Button lost_b = (Button) findViewById(R.id.lost_b);
		final Button found_b = (Button) findViewById(R.id.found_b);
		final Button posts_b = (Button) findViewById(R.id.posts_b);
		final Button logoff_b = (Button) findViewById(R.id.logoff_b);
		final Button sbc = (Button) findViewById(R.id.sbc);

		// Starts a new activity for submitting a lost item
		lost_b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, LostItemListActivity.class);
				startActivity(i);
			}
		});

		// Starts a new activity for submitting a found item
		found_b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, FoundItemListActivity.class);
				startActivity(i);
			}
		});

		// Starts a new activity that lists all items
		posts_b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO: make a layout & activity for an "all items" list
				// Intent i = new Intent(context, LostItemListActivity.class);
				// startActivity(i);
			}
		});

		sbc.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, SearchByCategory.class);
				startActivity(i);
			}
		});

		// Logs the user out and closes the app.
		logoff_b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				GLOBAL.logoff();
				finish();
			}
		});

		// === Admin UI ===

		if (GLOBAL.getCurrentUser().isAdmin()) {
			final TextView admin_options_tv = (TextView) findViewById(R.id.admin_options_tv);
			final Button remove_user_b = (Button) findViewById(R.id.remove_user_b);
			final Button create_new_admin_b = (Button) findViewById(R.id.create_new_admin_b);
			final Button unlock_user_b = (Button) findViewById(R.id.unlock_user_b);

			admin_options_tv.setVisibility(View.VISIBLE);
			remove_user_b.setVisibility(View.VISIBLE); // Does nothing yet
			create_new_admin_b.setVisibility(View.VISIBLE);
			unlock_user_b.setVisibility(View.VISIBLE);

			// Launches a new activity to create a new admin
			create_new_admin_b.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(context, CreateNewAdminActivity.class);
					startActivity(i);
				}
			});

			// This button opens a prompt for a username, then unlocks that user
			// if he exists.
			unlock_user_b.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					final EditText username_et = new EditText(context);

					// Creates a new dialog box with an EditText for the
					// username and two Buttons: unlock and cancel.
					new AlertDialog.Builder(context)
							.setTitle("Unlock User")
							.setMessage("Please enter the username:")
							.setView(username_et)
							.setPositiveButton("Unlock",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											try {
												GLOBAL.unlockUser(username_et
														.getText().toString());
												makeText(
														context,
														R.string.user_unlocked_toast,
														LENGTH_LONG).show();
											} catch (LoginHandler.UserNotFoundException unfe) {
												makeText(
														context,
														R.string.user_not_found_toast,
														LENGTH_LONG).show();
											}
										}
									})
							.setNegativeButton("Cancel",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											// Do nothing.
										}
									}).show();

				}
			});

			// This button opens a prompt for a username, then unlocks that user
			// if he exists.
			remove_user_b.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					final EditText username_et = new EditText(context);

					// Creates a new dialog box with an EditText for the
					// username and two Buttons: unlock and cancel.
					new AlertDialog.Builder(context)
							.setTitle("Remove User")
							.setMessage("Please enter the username:")
							.setView(username_et)
							.setPositiveButton("Remove",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											if (GLOBAL.deleteUser(username_et
													.getText().toString())) {
												makeText(
														context,
														R.string.user_removed_toast,
														LENGTH_LONG).show();
											} else {
												makeText(
														context,
														R.string.user_removed_failed_toast,
														LENGTH_LONG).show();
											}
										}
									})
							.setNegativeButton("Cancel",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											// Do nothing.
										}
									}).show();

				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.dashboard, menu);
		return true;
	}
}
